package com.itheima.reggie.controller;


import com.itheima.reggie.common.OssTemplate;
import com.itheima.reggie.common.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController     //@ResponseBody + @Controller
@Slf4j  //记录日志信息
public class FileController {

    //依赖注入OSS模板
    @Autowired
    private OssTemplate ossTemplate;

    /**
     * 文件上传
     * Request URL: http://localhost:8080/common/upload
     * Request Method: POST
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/common/upload")
    public ResultInfo uploadFile(@RequestBody MultipartFile file) throws IOException {       //接收在请求体的参数
        //1.调用阿里OSS的模板上传方法
        String filePath = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
        //2. 返回响应
        return ResultInfo.success(filePath);
    }
}
